/*
 * SoundEffects.java
 * Author: Ben Gentry
 * Date Completed: 28 June, 2012
 * 
 * Purpose: This module allows for sound effects to be played that correspond
 * to different events during the operation of the slot machine.
*/
package slotMachinePackage;

import java.io.*;
import javax.sound.sampled.*;

public class SoundEffects{
    
    public enum SoundEffect {
    COIN("coins.wav"),
    SCREAM("scream.wav"),
    HANDLE("handle.wav"),
    DROP("drop.wav");
    
        // nested class for specifying volume
        public static enum Volume {
            MUTE, LOW, MEDIUM, HIGH
        }

        public static SoundEffects.SoundEffect.Volume volume = SoundEffects.SoundEffect.Volume.LOW;

        // clip for each sound file
        private Clip clip;

        // constructor for sound file
        AudioInputStream audioInputStream = null;
        SoundEffect(String file) {
            try {
                File soundFile = new File(file);
                AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
                // Get a clip resource.
                clip = AudioSystem.getClip();
                // Open audio clip and load samples from the audio input stream.
                clip.open(audioIn);
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            }
        }

        // play sound effect from the beginning
        public void play() {
            if (volume != SoundEffects.SoundEffect.Volume.MUTE) {
                if (clip.isRunning())
                    clip.stop();
                clip.setFramePosition(0);
                clip.start();
            }
        }

        // pre-load sound files.
        static void init() {
            values();
        }
    }
}